package com.zw.mes.module.assemble.service;

import com.zw.mes.entity.asse.QrParse;
import com.zw.mes.entity.sys.User;
import com.zw.mes.module.assemble.entity.*;
import com.zw.mes.module.assemble.mapper.ReceiveMapper;
import com.zw.mes.module.assemble.mapper.TestingMapper;
import com.zw.mes.module.assemble.model.TestModel;
import com.zw.mes.module.assemble.repository.*;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Description: 装配申请质检 业务
 * @Author: China.wsg
 * @Date: 2021/04/23 14:12
 */
@Service
@SuppressWarnings("all")
public class ReceiveService {

    @Autowired
    private ReceiveMapper receiveMapper;
    @Autowired
    private AsseTransSheetRepository transSheetRepository;
    @Autowired
    private AsseTransCheckRepository transCheckRepository;
    @Autowired
    private AsseAssemblyRouteRepository assemblyRouteRepository;
    @Autowired
    private AsseAssemblySheetRepository assemblySheetRepository;
    @Autowired
    private AsseStoreRepository asseStoreRepository;
    @Autowired
    private TestingMapper testingMapper;
    @Autowired
    private LogService logService;

    /**
     * 查询待接收列表
     *
     * @param barCode 派工单二维码 规则：生产令号|图号|部分ID|2
     * @return
     */
    public List<TestModel> findRecListByBarCode(String barCode) {
        if (barCode == null || barCode.equals("")) {
            barCode = "1|1|1|2";// 页面打开时避免，空指针崩溃特殊处理
        }
        QrParse parse = getQrParse(barCode);
        if (parse == null) return null;
        return receiveMapper.findRecListByBarCode(parse);
    }

    /**
     * 字符串截取
     *
     * @param code
     * @return
     */
    private QrParse getQrParse(String code) {
        String[] arr = code.split("\\|");
        if (arr == null || arr.length < 4) {
            return null;
        }
        QrParse parse = new QrParse();
        parse.setProNo(arr[0]);
        parse.setDrawNo(arr[1]);
        parse.setPartId(arr[2]);
        parse.setCategory(arr[3]);
        return parse;
    }

    /**
     * 获取装配派工单信息
     *
     * @param barCode 派工单二维码 规则：生产令号|图号|部分ID|2
     * @return
     */
    public AsseAssemblySheet findAsseSheetByCode(String barCode) {
        if (barCode == null || barCode.equals("")) {
            barCode = "1|1|1|2";// 页面打开时避免，空指针崩溃特殊处理
        }
        QrParse parse = getQrParse(barCode);
        if (parse == null) return null;
        return receiveMapper.findAsseSheetByCode(parse);
    }

    /**
     * 转序接收
     */
    @Transactional
    public int receive(Integer id) {
        // 装配转序单表 与 装配质检表 存在一一对应的关系
        AsseTransSheet t = transSheetRepository.getOne(id);
        AsseTransCheck tc = transCheckRepository.findByTransId(t.getId());

        if (tc.getStatus() != 2) {
            return 0;
        }
        User u = ShiroUtils.getUser();
        t.setRecCode(u.getAccount());
        t.setRecName(u.getName());
        t.setRecTime(new Date());
        t.setRecDept(u.getBranchName());
        tc.setStatus(3);
        transSheetRepository.save(t);
        transCheckRepository.save(tc);

        AsseAssemblyRoute cur = assemblyRouteRepository.getOne(t.getRouteId());
        //获取下一道序
        AsseAssemblyRoute next = testingMapper.findNextRoute(cur);
        next.setRecNum(next.getRecNum() + t.getNum());
        next.setMayApplyNum(next.getMayApplyNum() + t.getNum());
        next.setStatus(2);

        AsseAssemblySheet sheet = assemblySheetRepository.getOne(cur.getSheetId());
        logService.saveLog("派工单-转序接收", "转序单ID：" + t.getId() + "，生产令号：" + sheet.getProdNo()
                + "<br>项目名称：" + sheet.getProName() + "<br>类型：" + sheet.getSpecial() + "<br>部分：" + sheet.getPart()
                + "<br>装配图号：" + sheet.getDrawNo() + "<br>装配图名：" + sheet.getDrawName() + "<br>工序：" + cur.getRoute()
                + "<br>接收数量：" + t.getNum());
        return receiveMapper.updateRouteRecNum(next);
    }

    /**
     * 派工单-历史接收列表
     *
     * @param testModel
     * @return
     */
    public List<TestModel> findHistoryRecListByQuery(TestModel testModel) {
        testModel.setRecCode(ShiroUtils.getUser().getAccount());
        return receiveMapper.findHistoryRecListByQuery(testModel);
    }

    /**
     * 撤销转序接收
     *
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int cancelRec(Integer id) {

        // 装配转序单表 与 装配质检表 存在一一对应的关系
        AsseTransSheet t = transSheetRepository.getOne(id);
        AsseTransCheck tc = transCheckRepository.findByTransId(t.getId());

        if (tc.getStatus() != 3) {
            return 0;
        }

        t.setRecCode(null);
        t.setRecName(null);
        t.setRecTime(null);
        t.setRecDept(null);
        tc.setStatus(2);
        transSheetRepository.save(t);
        transCheckRepository.save(tc);

        AsseAssemblyRoute cur = assemblyRouteRepository.getOne(t.getRouteId());
        //获取下一道序
        AsseAssemblyRoute next = testingMapper.findNextRoute(cur);
        next.setRecNum(next.getRecNum() - t.getNum());
        next.setMayApplyNum(next.getMayApplyNum() - t.getNum());
        next.setStatus(1);

        AsseAssemblySheet sheet = assemblySheetRepository.getOne(cur.getSheetId());
        logService.saveLog("派工单-转序撤销", "转序单ID：" + t.getId() + "，生产令号：" + sheet.getProdNo()
                + "<br>项目名称：" + sheet.getProName() + "<br>类型：" + sheet.getSpecial() + "<br>部分：" + sheet.getPart()
                + "<br>装配图号：" + sheet.getDrawNo() + "<br>装配图名：" + sheet.getDrawName() + "<br>工序：" + cur.getRoute()
                + "<br>接收数量：" + t.getNum());
        return receiveMapper.updateRouteRecNum(next);
    }

}
